package org.lee.web.controller.auth;

import cn.dev33.satoken.annotation.SaCheckLogin;
import org.lee.common.constant.Constants;
import org.lee.common.core.vo.request.LoginBody;
import org.lee.common.core.vo.response.AjaxResult;
import org.lee.common.util.LoginHelper;
import org.lee.system.model.SysMenu;
import org.lee.system.service.SysMenuService;
import org.lee.web.service.AuthService;
import org.noear.solon.annotation.*;
import org.noear.solon.validation.annotation.Validated;

import java.util.List;

@Controller
public class AuthController {

    @Inject
    private AuthService authService;

    @Inject
    private SysMenuService menuService;

    @Mapping("/login")
    @Post
    public AjaxResult pcLogin(@Validated LoginBody body) {
        AjaxResult ajax = AjaxResult.success();
        // 生成令牌
        String token = authService.login(body);
        ajax.put(Constants.TOKEN, token);
        return ajax;
    }

    /**
     * 获取用户信息
     *
     * @return 用户信息
     */
    @SaCheckLogin
    @Get
    @Mapping("/getInfo")
    public AjaxResult getInfo() {
        return authService.getInfo();
    }

    /**
     * 获取路由信息
     *
     * @return 路由信息
     */
    @SaCheckLogin
    @Get
    @Mapping("getRouters")
    public AjaxResult getRouters() {
        Long userId = LoginHelper.getLoginUser().getUserId();
        List<SysMenu> menus = menuService.selectMenuTreeByUserId(userId);
        return AjaxResult.success(menuService.buildMenus(menus));
    }
}
